﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AviFile;
using DissipativeInputProcessing;
using MessageBox = System.Windows.MessageBox;

namespace ComputationVisualizer
{
    /// <summary>
    /// Interaction logic for VideoExportingWindow.xaml
    /// </summary>
    public partial class VideoExportingWindow : Window
    {
        public VideoExportingWindow()
        {
            InitializeComponent();
        }

        private IList<IFrame> _frames;
        private IFramePool _pool;
        double fps, startFrame, endFrame, framesPerCadre;
        string filePath;
        public IFramePool Frames {
            get { return _pool; }
            set
            {
                _pool = value;
                _frames = _pool.Frames;
                endFrameTB.Text = (_frames.Count-1).ToString();
            }
        }
        void submitButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                filePath = pathTextBox.Text;
                if(string.IsNullOrEmpty(filePath))
                    throw new Exception("Не задано имя файла.");
                fps = Double.Parse(fpsTextBox.Text);
                startFrame = Double.Parse(startFrameTB.Text);
                endFrame = Double.Parse(endFrameTB.Text);
                framesPerCadre = Double.Parse(framesPerCadrTextBox.Text);
                if (endFrame > (_frames.Count - 1))
                    throw new Exception("Слишком большое значение финального кадра.");
                if (startFrame > (_frames.Count - 1))
                    throw new Exception("Слишком большое значение финального кадра.");
                if (endFrame <=startFrame)
                    throw new Exception("Некорректно заданы начальный и конечный кадр.");
                //if(autoSizeChB.IsChecked==true)
                //{
                //    width = Frames[0].Image.Width;
                //    height = Frames[0].Image.Height;
                //    for(int i=1;i<Frames.Count;i++)
                //    {
                //        if (height < Frames[i].Image.Height)
                //            height = Frames[i].Image.Height;
                //        if (width < Frames[i].Image.Width)
                //            width = Frames[i].Image.Width;
                //    }
                //}
                //else
                //{
                //    width = int.Parse(widthTB.Text);
                //    height = int.Parse(heightTB.Text);
                //}
            }
            catch (FormatException)
            {
                MessageBox.Show("Некорректные параметры.");
                return;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            DialogResult = true;
        }

        public void Export()
        {
            var manager = new AviManager(filePath, false);
            var stream = manager.AddVideoStream(true, fps, _pool.GetFrame(startFrame).Image);
            var frameCounter = startFrame + framesPerCadre;
            while (frameCounter < endFrame)
            {
                stream.AddFrame(_pool.GetFrame(frameCounter).Image);
                frameCounter += framesPerCadre;
            }
            stream.Close();
            manager.Close();
        }

        private void browseBtn_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new SaveFileDialog();
            dialog.Filter = "AVI files (*.avi)|*.avi";
            dialog.FilterIndex = 0;
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                pathTextBox.Text = dialog.FileName;
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        //private void autoSizeChB_Checked(object sender, RoutedEventArgs e)
        //{
        //    widthTB.IsEnabled = false;
        //    heightTB.IsEnabled = false;
        //}

        //private void autoSizeChB_Unchecked(object sender, RoutedEventArgs e)
        //{
        //    widthTB.IsEnabled = true;
        //    heightTB.IsEnabled = true;
        //}
    }
}
